package com.cungmua.app.task;

import android.content.Context;

import com.cungmua.app.handler.BaseAuthenticatedRequestHandler;
import com.cungmua.app.handler.PlaceOrderHandler;
import com.cungmua.app.model.request.PlaceOrderRequest;
import com.cungmua.app.model.response.PlaceOrderResponse;
import com.cungmua.app.net.CungMuaAPI;

import retrofit.RetrofitError;

/**
 * Created by kelvin.nguyen on 6/25/14.
 */
public class PlaceOrderTask extends AuthenticatedTask<PlaceOrderRequest, Void, PlaceOrderResponse>{
    Context context;
    PlaceOrderHandler handler;

    public PlaceOrderTask(Context context, PlaceOrderHandler handler) {
        this.context = context;
        this.handler = handler;
    }

    @Override
    protected Context getContext() {
        return context;
    }

    @Override
    protected PlaceOrderResponse doInBackground() throws RetrofitError {
        PlaceOrderRequest request = params[0];

        return CungMuaAPI.getService().placeOrder(request.toFieldMap());
    }

    @Override
    protected BaseAuthenticatedRequestHandler getHandler() {
        return handler;
    }

    @Override
    protected void onSuccess(PlaceOrderResponse placeOrderResponse) {
        handler.orderPlaced(placeOrderResponse);
    }
}

